#include "..\Include\WidgetReplay.h"


WidgetReplay::WidgetReplay(const char *pRootPath, const char *pIniUIFilename, const char *pIniDriverMappingFilename, const char *pIniVehiculeClassMappingFilename)
{
	initialized = false;
	rootPath = pRootPath; 

	//nbProfiles = GetPrivateProfileInt("Profiles", "count", 0, pIniUIFilename);
	//for (int i = 0 ; i < nbProfiles; i++) {
	//	sprintf(tmp, "Profile.%d", (i+1));
	//	profiles[i].widgetOffsetX = (signed int)GetPrivateProfileInt(tmp, "UIInfo.offsetX", 0, pIniUIFilename);
	//	profiles[i].widgetOffsetY = (signed int)GetPrivateProfileInt(tmp, "UIInfo.offsetY", 0, pIniUIFilename);
	//	profiles[i].hide = GetPrivateProfileInt(tmp, "UIInfo.hide", 0, pIniUIFilename) != 0;
	//}

	//std::string str = rootPath;
	GetPrivateProfileString("UI Widgets", "Replay.ini", "", tmp, 999, pIniUIFilename);
	//str.append(tmp);
	sprintf(iniUIFilename, "%s%s", rootPath, tmp);
	//iniUIFilename = str.c_str(); //pIniUIFilename; 
	available = GetPrivateProfileInt("Replay", "available", 0, iniUIFilename) != 0;

	lastMETForComputation = -1;

}

void WidgetReplay::init(const ScreenInfoV01 &info, float ratio, float ratioWidth, float ratioHeight, Sprite spriteList[]) {
	if (!available) {
		initialized = true;
		return;
	}
	lastMETForComputation = -1;

	//for (int i = 0 ; i < nbProfiles; i++) {
	//	profiles[i].widgetOffsetX *= ratioWidth;
	//	profiles[i].widgetOffsetY *= ratioHeight;
	//}

	int posX = (int)(ratioWidth * GetPrivateProfileInt("Replay", "image.left", 40, iniUIFilename));//40;
	int posY = (int)(ratioHeight * GetPrivateProfileInt("Replay", "image.top", 550, iniUIFilename));//550;

	blinking = GetPrivateProfileInt("Replay", "blinking", 1, iniUIFilename) == 1;
	shownDurationInSecond = ((double)GetPrivateProfileInt("Replay", "shown.duration.millisecond", 1000, iniUIFilename)) / 1000.0;
	hiddenDurationInSecond = ((double)GetPrivateProfileInt("Replay", "hidden.duration.millisecond", 500, iniUIFilename)) / 1000.0;

	//LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	

	bgImageData.spriteIndex = GetPrivateProfileInt("Replay", "image.id", 0, iniUIFilename) -1;
	if (bgImageData.spriteIndex > -1) {
		bgImageData.left = posX;
		bgImageData.top = posY;
		bgImageData.width = (int)(GetPrivateProfileInt("Replay", "image.width", (int)spriteList[bgImageData.spriteIndex].width, iniUIFilename) * ratioWidth);
		bgImageData.height = (int)(GetPrivateProfileInt("Replay", "image.height", (int)spriteList[bgImageData.spriteIndex].height, iniUIFilename) * ratioHeight);
		bgImageData.colorIndex = GetPrivateProfileInt("Replay", "image.color.id", 1, iniUIFilename) -1;
	}
}

void WidgetReplay::render(int profileIndex, const ScreenInfoV01 &info, const LPD3DXFONT *fontList, const D3DCOLOR *colorList, Sprite spriteList[], double mET) { 

	if (!available) return;
	if (!gReplayIsActive) return;
	//if (profiles[profileIndex].hide) return;

	if (blinking) {
		if (lastMETForComputation <= 0 || lastMETForComputation > mET || lastMETForComputation < mET - 2 * (shownDurationInSecond + hiddenDurationInSecond)) {
			lastMETForComputation = mET;
		} else if ( lastMETForComputation + shownDurationInSecond + hiddenDurationInSecond < mET ) {
			lastMETForComputation += shownDurationInSecond + hiddenDurationInSecond;
		}

		if (mET - lastMETForComputation >= shownDurationInSecond) {
			return;
		}
	}


	LPDIRECT3DDEVICE9 dev = static_cast<LPDIRECT3DDEVICE9>(info.mDevice);	
	if (bgImageData.spriteIndex > -1) {
		spriteList[bgImageData.spriteIndex].setPosition(bgImageData.left /*+  profiles[profileIndex].widgetOffsetX*/, bgImageData.top /*+ profiles[profileIndex].widgetOffsetY*/);
		spriteList[bgImageData.spriteIndex].setSize(bgImageData.width, bgImageData.height);
		spriteList[bgImageData.spriteIndex].render(dev, colorList[bgImageData.colorIndex], 100);
	}
}

WidgetReplay::~WidgetReplay(void)
{
}
